#encoding=utf8
'''
Created on 2012-9-17

@author: Administrator
'''
from pub import Func
from pub.Config import Config
try:
    from ULClass import ULClass
    from URClass import URClass
    from UPMClass import UPMClass
    from UIMClass import UIMClass;
    from RIClass import RIClass
    from RUClass import RUClass;
    from RGAClass import RGAClass;
    from RISGClass import RISGClass;
    from RIGClass import RIGClass;
    from RICClass import RICClass;
    from RIFUClass import RIFUClass;
    from RINUClass import RINUClass;
    from RIFAClass import RIFAClass;
    from RIFACClass import RIFACClass;
    from RIUEClass import RIUEClass;
    from RIKOClass import RIKOClass;
    from RISOClass import RISOClass;
    from UFLClass import UFLClass;
    from UFFAClass import UFFAClass;
    from UFSAClass import UFSAClass;
    from USDClass import USDClass;
    from UFNUClass import UFNUClass;
    from UFCClass import UFCClass;
    from UFSClass import UFSClass;
    from UPSClass import UPSClass;
    from SVCClass import SVCClass;
    from GTClass import GTClass;
    from RICUClass import RICUClass;
except Exception,ex:
    Func.debugInfo("CommandParser import:"+Func.toStr(ex));

class CommandParser(object):
    '''
    u'命令解析器，讲传入命令解析为list，然后根据listmap调用相对应的命令逻辑类，并使用response方法返回解析结果
    example:
        cmd = "LOGIN#1#geminiblue#gemini4094"
        commander = CommandPaser(cmd)
        result = commander.response()
    '''
    def __init__(self,sockObj,data):
        '''
        Constructor
        '''
        self._sockObj = sockObj
        self._cmdMap=Config.CommandMap
        self._cmd = None
        self._cmdId = None
        self._cmdContent = None
        if data!="" and data!=None:
            self._data = data            
        else:
            pass
        pass
        
    def splitCommand(self):
        '''
        u'将命令字符串分解为list,并得到要执行的命令，命令Id，命令内容
        '''
        
        if(str(self._data).find("#")>0):
            data = self._data.split('#')
            self._cmd = data[0]
            print Config.CommandMap.has_key(str(self._cmd))
            if(not Config.CommandMap.has_key(self._cmd)):
                return False
            self._cmdId = data[1]
            self._cmdContent = self.getCmdContent(data)
            return True
        else:
            return False
    def getCmdContent(self,data):
        data.pop(0)
        data.pop(0)
        return data  
    def getCommandRelation(self):
        pass
    def callCommand(self,):
        '''
        u'根据cmdmap调用相应的命令执行逻辑类
        '''
        test = self.splitCommand()
        Func.debugInfo(test)
        if(test):
            tmpClass = None
            try:
                tmpClass = globals()[self._cmdMap[self._cmd][1]]()
                tmpClass.getData(self._cmd,self._cmdId,self._cmdContent,self._sockObj)
                return tmpClass.outData()
            except Exception,ex:                
                Func.debugInfo(Func.toStr(ex));
                Func.sendError(self._sockObj, Config.SYS_CALL_COMMAND_ERROR, False);
                return
        else:
            Func.sendError(self._sockObj, Config.SYS_COMMAND_NOT_FOUND, False)
            return

    def response(self,):
        '''
        u'返回命令逻辑类执行后的结果
        '''
        return self.callCommand()
if __name__=="__main__":
        cmd = "UL#1#geminiblue#gemini4094"
        commander = CommandParser(None,cmd)
        result = commander.response()